﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlDieuChinhDonHangDaIn : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadChiNhanh();
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        BindGrid(ddlChiNhanh.SelectedValue.ToString());
    }
    void BindGrid(string sitecode)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_DonHangDaIn", CommandType.StoredProcedure,
                new SqlParameter("@Sitecode", sitecode));
            if (db.Rows.Count > 0)
            {
                grvOrderPrinted.DataSource = db;
            }
            else
            {
                grvOrderPrinted.DataSource = null;
                uscMsgBox1.AddMessage("Không có đơn hàng nào đã in", uscMsgBox.enmMessageType.Error);
            }
            grvOrderPrinted.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    void LoadChiNhanh()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
            dt.BindDropDownList(db, ddlChiNhanh, "Sitecode", "sitename");
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void grvOrderPrinted_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         dt=new ClassExecute.ClassExecute();
         try
         {
             if (e.CommandName == "chon")
             {
                 GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                 Label lblSoHD = (Label)grv.FindControl("lblSoHD");

                 bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "k_openTrannoPrinted", ref error,
                     new SqlParameter("@tranno", lblSoHD.Text),
                     new SqlParameter("@RecUser", Session["user"].ToString()));
                 if (kq)
                 {
                     uscMsgBox1.AddMessage("Điều chỉnh đơn hàng số hóa đơn " + lblSoHD.Text + " thành công", uscMsgBox.enmMessageType.Success);
                     BindGrid(ddlChiNhanh.SelectedValue.ToString());
                 }
                 else
                 {
                     uscMsgBox1.AddMessage("Hệ thống đang bảo trì", uscMsgBox.enmMessageType.Error);
                 }
             }
         }
         catch (Exception ex)
         {

             string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
             dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
         }    
    }
    protected void grvOrderPrinted_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvOrderPrinted.PageIndex = e.NewPageIndex;
        BindGrid(ddlChiNhanh.SelectedValue.ToString());
    }
}